<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <el-form-item label="资源名称" prop="name">
        <el-input
          v-model="queryParams.name"
          class="!w-240px"
          clearable
          placeholder="请输入资源名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="contractNumber">
        <el-input
          v-model="queryParams.contractNumber"
          class="!w-240px"
          clearable
          placeholder="请输入合同编号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="版权编号" prop="copyrightNumber">
        <el-input
          v-model="queryParams.copyrightNumber"
          class="!w-240px"
          clearable
          placeholder="请输入版权编号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="openForm('新增')">
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          查找
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['system:role:create']" plain type="primary" @click="handleActive()">
          <Icon class="mr-5px" icon="ep:plus" />
          从ERP中导入
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="ID" prop="id" :width="50" />
      <el-table-column align="center" label="资源名称" prop="name" show-overflow-tooltip />
      <el-table-column align="center" label="资源类型" prop="type" :width="100" />
      <el-table-column
        align="center"
        label="合同编号"
        prop="contractNumber"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="版权编号"
        prop="copyrightNumber"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="版权有效期" prop="copyrightValidity" :width="200" />
      <el-table-column align="center" label="版权状态" prop="copyrightStatus">
        <template #default="scope">
          <el-tag v-if="scope.row.copyrightStatus == '正常'" type="success">{{
            scope.row.copyrightStatus
          }}</el-tag>
          <el-tag v-else="scope.row.copyrightStatus == '已过期'" type="danger">{{
            scope.row.copyrightStatus
          }}</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="结算阶段" prop="settlementStage" :width="90" /> -->
      <!-- <el-table-column align="center" label="结算状态" prop="settlementStatus" :width="90" /> -->
      <el-table-column :width="160" align="center" label="操作">
        <template #default="scope">
          <el-button
            v-hasPermi="['system:role:update']"
            link
            type="primary"
            @click="openForm('查看详情', scope.row.id)"
          >
            查看
          </el-button>
          <el-button
            v-hasPermi="['system:role:update']"
            link
            type="primary"
            @click="openForm('编辑', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            v-hasPermi="['system:role:delete']"
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
          <el-button
            v-hasPermi="['system:role:update']"
            link
            type="primary"
            @click="openStageForm(scope.row.id)"
          >
            阶段结算
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <!--  <CopyrightForm ref="formRef" @success="getList" />-->
  <!-- 表单弹窗：阶段结算弹窗 -->
  <stageForm ref="refStageForm" @success="getList" />
</template>
<script lang="ts" setup>
import * as CopyrightApi from '@/api/copyright/copyright'
import stageForm from './details/stageForm.vue'

defineOptions({ name: 'Copyright' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { push } = useRouter() // 路由

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: '',
  contractNumber: '',
  copyrightNumber: '',
  createTime: []
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CopyrightApi.getCopyrightPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (type: string, id?: number) => {
  if (type === '新增') {
    push({
      name: 'addCopyRight',
      query: {
        type: type,
        id: id
      }
    })
  } else {
    push({
      name: 'updateCopyRight',
      query: {
        type: type,
        id: id
      }
    })
  }
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CopyrightApi.deleteCopyright(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 功能未开发按钮！ */
const handleActive = async () => {
  return message.success(t('功能未开发！'))
}

/** 阶段结算弹窗 */
const refStageForm = ref()
const openStageForm = async (id: number) => {
  refStageForm.value.open(id)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
